package com.gitee.fitness.controller;

import com.gitee.fitness.config.RabbitMqConfig;
import com.gitee.fitness.model.MqttRequestBody;
import com.gitee.fitness.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/mq")
@Slf4j
public class MqController {

    @Resource
    private IMessageService messageService;

    @RequestMapping("/send")
    public String sendMessage(@RequestBody MqttRequestBody mqttRequestBody) {
        try {
            messageService.sendMessage(RabbitMqConfig.EXCHANGE_NAME, mqttRequestBody.getTopic(), mqttRequestBody.getPayload());
            return "发送成功";
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "发送失败";
        }
    }
}
